import bayes_optimize as knockouts
import numpy as np

knockout_teams = knockouts.top_teams


def simulate_match(_team1, _team2):
    """基于胜率模拟比赛结果"""
    data1 = knockout_teams[_team1]
    data2 = knockout_teams[_team2]

    # 归一化比赛成绩胜率
    base_prob1 = data1['qualify16'] / (data1['qualify16'] + data2['qualify16'])
    base_prob2 = data2['qualify16'] / (data1['qualify16'] + data2['qualify16'])

    # 加入FIFA排名修正（排名越高修正系数越小）排名差转换为影响系数（每差1名影响0.5%）
    rank_diff = data2['fifa_rank'] - data1['fifa_rank']  # 正数表示team1排名更高
    rank_factor = 1 + (rank_diff * 0.05)  # 排名每高1位，胜率+0.5%

    # 综合计算最终概率
    final_prob1 = base_prob1 * rank_factor
    final_prob2 = base_prob2 / rank_factor  # 对称调整

    # 再次归一化处理
    final_prob1 /= (final_prob1 + final_prob2)
    final_prob2 /= (final_prob1 + final_prob2)

    # 再加入随机因素,爆冷情况(5%波动)
    random_factor = np.random.normal(1, 0.05)

    print('主队:' + str(random_factor * final_prob1) + ',客队:' + str(final_prob2))
    # 判定结果
    if random_factor * final_prob1 > final_prob2:
        return _team1
    else:
        return _team2


# 1/8决赛
print("=" * 40)
print("1/8决赛对阵及预测结果：")
print("=" * 40)
round_of_16 = [
    ("A1", "B2"), ("B1", "A2"),
    ("C1", "D2"), ("D1", "C2"),
    ("E1", "F2"), ("F1", "E2"),
    ("G1", "H2"), ("H1", "G2")
]

quarter_finalists = []
for i, (team1, team2) in enumerate(round_of_16, 1):
    winner = simulate_match(team1, team2)
    quarter_finalists.append(winner)
    print(
        f"比赛{i}: {knockout_teams[team1]['team']} vs {knockout_teams[team2]['team']} "
        f"→ 胜者: {knockout_teams[winner]['team']}")

# 1/4决赛
print("\n" + "=" * 40)
print("1/4决赛对阵及预测结果：")
print("=" * 40)
qf_matches = [
    (quarter_finalists[0], quarter_finalists[2]),
    (quarter_finalists[1], quarter_finalists[3]),
    (quarter_finalists[4], quarter_finalists[6]),
    (quarter_finalists[5], quarter_finalists[7])
]

semi_finalists = []
for i, (team1, team2) in enumerate(qf_matches, 1):
    winner = simulate_match(team1, team2)
    semi_finalists.append(winner)
    print(f"比赛{i}: {knockout_teams[team1]['team']} vs {knockout_teams[team2]['team']} "
          f"→ 胜者: {knockout_teams[winner]['team']}")
# 半决赛
print("\n" + "=" * 40)
print("半决赛对阵及预测结果：")
print("=" * 40)
sf_matches = [
    (semi_finalists[0], semi_finalists[2]),  # 上半区决赛名额争夺
    (semi_finalists[1], semi_finalists[3])  # 下半区决赛名额争夺
]

# 半决赛模拟
finalists = []
third_place = []
for i, (team1, team2) in enumerate(sf_matches, 1):
    winner = simulate_match(team1, team2)
    loser = team2 if winner == team1 else team1
    finalists.append(winner)
    third_place.append(loser)
    print(f"半决赛{i}: {knockout_teams[team1]['team']} vs {knockout_teams[team2]['team']} "
          f"→ 胜者: {knockout_teams[winner]['team']}")

# 季军赛（两场半决赛的败者对决）
print("\n" + "=" * 40)
print("季军赛预测结果：")
print("=" * 40)
third_winner = simulate_match(third_place[0], third_place[1])
print(f"季军赛: {knockout_teams[third_place[0]]['team']} vs {knockout_teams[third_place[1]]['team']} "
      f"→ 季军: {knockout_teams[third_winner]['team']}")

# 决赛（两场半决赛的胜者对决）
print("\n" + "=" * 40)
print("决赛预测结果：")
print("=" * 40)
champion = simulate_match(finalists[0], finalists[1])
runner_up = finalists[1] if champion == finalists[0] else finalists[0]
print(f"决赛: {knockout_teams[finalists[0]]['team']} vs {knockout_teams[finalists[1]]['team']} "
      f"→ 冠军: {knockout_teams[champion]['team']}"f"  亚军: {knockout_teams[runner_up]['team']}")


